<?php

class UserAction extends PublicAction
{
    function index()
    {
        import('@.ORG.Page');

        $keyword = $_GET['keyword'];
        $searchtype = $_GET['searchtype'];
        $groupId = intval($_GET['group_id']);

        $this->assign($_GET);

        if (!empty($keyword) && !empty($searchtype)) {
            $where[$searchtype] = array('like', '%' . $keyword . '%');
        }
        if ($groupId) {
            $where['group_id'] = $groupId;
        }

        $userModel = D('User');
        $count = $userModel->where($where)->count();
        $page = new Page($count, 20);
        $show = $page->show();
        $this->assign("page", $show);

        $list = $userModel->relation('UserGroup')
            ->order('id')
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        $this->assign('ulist', $list);

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    /**
     * 更新
     *
     */
    function edit()
    {
        $userModel = D('User');
        if (IS_POST) {
            $_POST['password'] = $_POST['password'] ? sysmd5($_POST['password']) : $_POST['opassword'];

            if ($data = $userModel->create()) {
                if (!empty($data['id'])) {
                    if (false !== $userModel->save()) {
                        $this->success(L('add_ok'), cookie('__forward__'));
                    } else {
                        $this->error(L('edit_error') . $userModel->getDbError());
                    }
                } else {
                    $this->error(L('do_error'));
                }
            } else {
                $this->error($userModel->getError());
            }
        } else {
            $id = $_GET['id'];

            if (empty($id))
                $this->error(L('do_empty'));

            $member = $userModel->find($id);

            $this->assign('member', $member);

            $userGroups = M('UserGroup')->select();

            $this->assign('userGroups', $userGroups);

            $this->display();
        }

    }


    function add()
    {
        if (IS_POST) {
            $userModel = D('User');

            if ($data = $userModel->create()) {
                if (false !== $userModel->add()) {
                    $this->success(L('add_ok'), cookie('__forward__'));
                } else {
                    $this->error(L('add_error'));
                }
            } else {
                $this->error($userModel->getError());
            }
        } else {
            $userGroups = M('UserGroup')->select();

            $this->assign('userGroups', $userGroups);
            $this->display();
        }
    }

    function delete()
    {
        $userModel = M('User');
        $id = I('get.id');
        if (!$id) {
            $this->error('参数不正确！');
        }

        if (false !== $userModel->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败: ' . $userModel->getDbError());
        }
    }
}